<?php
class MantaHomePage extends MantaPage {
	var $name = "Home";
	protected $table;
	protected $current_status_table;
	protected $current_status_image = array();
	protected $current_status_button = array();

	function create_frame() {
		$this->table = new MuiTable();
		$current_status_frame = new GtkFrame("Current status");
		$this->current_status_table = new MuiTable();
		$this->current_status_table->set_col_spacings(0);
		$this->current_status_table->set_row_spacings(0);
		$current_status_frame->add($this->current_status_table);
		$this->table->attach_simple($current_status_frame, 1, 1, Gtk::SHRINK, Gtk::SHRINK);

		$this->update_status();

		return $this->table;
	}

	protected function new_button($running, $id, $row) {
		if ($running)
			$button = new GtkButton("Stop");
		else
			$button = new GtkButton("Start");
		$button->connect_simple("clicked", array($this, "change_running_status"), $id, $row);
		return $button;
	}

	protected function new_image($running) {
		if ($running)
			$image = GtkImage::new_from_stock(Gtk::STOCK_YES, Gtk::ICON_SIZE_SMALL_TOOLBAR);
		else
			$image = GtkImage::new_from_stock(Gtk::STOCK_NO, Gtk::ICON_SIZE_SMALL_TOOLBAR);
		return $image;
	}

	function update_status() {
		global $package_manager;
		$row = 0;
		foreach ($package_manager->packages as $id=>$package) {
			$this->current_status_button[$id] = $this->new_button($package->is_running(), $id, $row);
			$this->current_status_image[$id] = $this->new_image($package->is_running());
			$label = new GtkLabel($package->name_display);
			$label->set_alignment(0, 0.5); // left align
			$this->current_status_table->attach_simple($this->current_status_image[$id],  1, $row+1);
			$this->current_status_table->attach_simple($this->current_status_button[$id], 2, $row+1);
			$this->current_status_table->attach_simple($label,                            3, $row+1);
			$row++;
		}
	}

	function change_running_status($id, $row) {
		global $package_manager, $tray;
		if ($package_manager->packages[$id]->is_running())
			$package_manager->packages[$id]->stop();
		else
			$package_manager->packages[$id]->start();

		if ($package_manager->packages[$id]->is_running()) {
			$this->current_status_button[$id]->set_label("Stop");
			$this->current_status_image[$id]->set_from_stock(Gtk::STOCK_YES, Gtk::ICON_SIZE_SMALL_TOOLBAR);
			$tray->set_status(true);
		} else {
			$this->current_status_button[$id]->set_label("Start");
			$this->current_status_image[$id]->set_from_stock(Gtk::STOCK_NO, Gtk::ICON_SIZE_SMALL_TOOLBAR);
			$tray->set_status(false);
		}
	}
}
